package com.stx.threeGrup.controller;

//import com.mysql.cj.Session;
import com.stx.threeGrup.mapper.UserMapperM;
import com.stx.threeGrup.model.Results;
import com.stx.threeGrup.model.automodel.User;
import com.stx.threeGrup.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/6/11
 *
 * @author Administrator
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/login")
    public ModelAndView login(String username, String password,ModelAndView mav, HttpSession session) {
        boolean login = userService.login(username,password);
        if (login) {
            if(getAllUser(session)==1) {
                User userInfo = userService.queryUserByUserName(username);
                session.setAttribute("user", userInfo);
                mav.setViewName("WEB-INF/houtai/index");
                return mav;
            }
        }
        mav.setViewName("administer_login");
        return mav ;
    }

    @RequestMapping("/login1")
    public @ResponseBody
    Results login1(String username, String password, HttpSession session) {
        System.out.println(username+password);
        boolean login = userService.login(username,password);
        if (login) {
            User userInfo = userService.queryUserByUserName(username);
            session.setAttribute("user",userInfo);
            return new Results("登录成功",1).addObject("user",userInfo);
        }
        return new Results("登录失败，或者用户名不存在",0);
    }

    @RequestMapping("/register")
    public @ResponseBody
    Results register (User user ,HttpSession session ){
        if(userService.register(user)>0)
        {
            session.setAttribute("userName",user.getUserUname());
            return new Results("注册成功",1);
        }
        return new Results("注册失败",0);
    }

    @RequestMapping("/delete")
    public  ModelAndView delete (int userId,HttpSession session,ModelAndView mav){
        System.out.println("a1");
        mav.setViewName("WEB-INF/houtai/index");
        if (userService.deleteUser(userId)>0)
        {
            System.out.println("b1");
            getAllUser(session);
        }
        return mav;
    }

    @RequestMapping("/queyUserByUserName")
    public @ResponseBody Results queyUserByUserName(HttpSession session,String userName){
        User userInfo = userService.queryUserByUserName(userName);
        session.setAttribute("user",userInfo);
        return new Results("获取成功",1).addObject("user",userInfo);
    }

    @RequestMapping("/queyUserAll")
    public @ResponseBody Results queyUserAll(){
        List<User> userList = userService.queryUserAll();
        return new Results("获取成功",1).addObject("userList",userList);
    }

    public @ResponseBody int  getAllUser(HttpSession session){
        List<User> userList = userService.queryUserAll();
        if(userList!=null){
            session.removeAttribute("userList");
            session.setAttribute("userList",userList);
            return 1;
        }
        return 0;
    }
}
